#ifndef FILM_TEST_H
#define FILM_TEST_H

#include <stdio.h>              /* printf */
#include <stdlib.h>             /* exit */
#include <sys/times.h>
#include <unistd.h>

#define TEST(x)                                                         \
  if (x) {                                                              \
    printf("%s - %2d, \"" #x "\" ==> ok\n", __FILE__, __LINE__);        \
  } else {                                                              \
    fprintf(stderr, "%s %2d, \"" #x "\" ==> ERROR\n", __FILE__, __LINE__); \
    exit(1);                                                            \
  }

#define TEST_SECTION(x) \
  puts("----- Section [ " x " ] start -----")

#define DOTIMES(__c__, __max__, __body__)       \
  {                                             \
    register int __c__ = 0;                     \
    for (;;) {                                  \
      if (__c__ < __max__) {                    \
        __body__;                               \
        __c__++;                                \
      } else                                    \
        break;                                  \
    }                                           \
  }

#define DOTIME(__max__, __body__)               \
  {                                             \
    register int __c__ = 0;                     \
    for (;;) {                                  \
      if (__c__ < __max__) {                    \
        __body__;                               \
        __c__++;                                \
      } else                                    \
        break;                                  \
    }                                           \
  }

void __fl_pt_times__(clock_t real, struct tms *tmsstart, struct tms *tmsend);

extern void __fl_err_doit__(const char *file, unsigned int line, const char *fmt, ...);
#define FL_SYSTEM_ERROR(...) { (__fl_err_doit__(__FILE__, __LINE__, __VA_ARGS__)); exit(1); }
  
#define TIME(body) {                                              \
    struct tms tmsstart, tmsend;                                  \
    clock_t start, end;                                           \
    if ((start = times(&tmsstart)) == -1)                         \
      FL_SYSTEM_ERROR("times error");                             \
    { body; }                                                     \
    if ((end = times(&tmsend)) == -1)                             \
      FL_SYSTEM_ERROR("times error");                             \
    __fl_pt_times__(end - start, &tmsstart, &tmsend);             \
  }

#define TIMEC(c, body) \
  { TIME(DOTIMES(_, c, body); fprintf(stderr, "loop: %d count -> ", c);); }

#endif  /* FILM_TEST_H */
